package com.xiao.hao.service;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.xiao.hao.convertor.XhMedicationPlanConvertor;
import com.xiao.hao.entity.XhMedicationPlan;
import com.xiao.hao.exception.BusinessException;
import com.xiao.hao.facade.XhMedicationPlanFacade;
import com.xiao.hao.mapper.XhMedicationPlanMapper;
import com.xiao.hao.req.XhMedicationPlanInsertReq;
import com.xiao.hao.req.XhMedicationPlanQueryPageReq;
import com.xiao.hao.req.XhMedicationPlanUpdateReq;
import com.xiao.hao.resp.XhMedicationPlanResp;
import com.xiao.hao.utils.BaseId;
import com.xiao.hao.utils.PageResult;
import com.xiao.hao.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Description 用户关联用药计划 服务实现层
 * @Author xiaohao
 * @Date 2025-03-05 17:23:15
 */
@Service
@Slf4j
public class XhMedicationPlanServiceImpl implements XhMedicationPlanFacade {

    @Resource
    XhMedicationPlanMapper mapper;


    /**
     * 功能描述: 角色新增
     *
     * @param req
     * @return com.xiao.hao.utils.Result
     * @author xiaohao
     * @date 2025-03-05 17:23:15
     */
    @Override
    public Result insert(XhMedicationPlanInsertReq req) {
        long count = mapper.selectList(new LambdaQueryWrapper<XhMedicationPlan>().eq(XhMedicationPlan::getMedicationName, req.getMedicationName())).stream().count();
        BusinessException.isTrue(count == 0, "用药计划名称重复，请重新添加！！！");
        XhMedicationPlan xhMedicationPlan = XhMedicationPlanConvertor.INSTANCE.reqToEntity(req);
        xhMedicationPlan.setProductDate(req.getProductDateList().stream().collect(Collectors.joining(",")));
        xhMedicationPlan.setMedicationNum(req.getProductDateList().size());
        return Result.succeed(mapper.insert(xhMedicationPlan));
    }


    /**
     * 功能描述: 角色修改
     *
     * @param req
     * @return com.xiao.hao.utils.Result
     * @author xiaohao
     * @date 2025-03-05 17:23:15
     */
    @Override
    public Result update(XhMedicationPlanUpdateReq req) {
        return Result.succeed(mapper.updateById(XhMedicationPlanConvertor.INSTANCE.reqToEntity(req)));
    }


    /**
     * 功能描述: 删除角色
     *
     * @param req
     * @return com.xiao.hao.utils.Result
     * @author xiaohao
     * @date 2025-03-05 17:23:15
     */
    @Override
    public Result delete(BaseId req) {
        return Result.succeed(mapper.deleteById(req.getId()));
    }

    /**
     * 功能描述: 角色分页查询
     *
     * @param req
     * @return com.xiao.hao.utils.PageResult
     * @author xiaohao
     * @date 2025-03-05 17:23:15
     */
    @Override
    public PageResult queryPage(XhMedicationPlanQueryPageReq req) {
        Page<XhMedicationPlan> page = mapper.selectPage(new Page(req.getNum(), req.getSize()), new LambdaQueryWrapper<XhMedicationPlan>()
                .eq(XhMedicationPlan::getUserInfoFileId, req.getUserInfoFileId())
                .orderByDesc(XhMedicationPlan::getCreateTime));
        List<XhMedicationPlanResp> xhMedicationPlanResps = Lists.newArrayList();
        if (ObjectUtil.isNotEmpty(page.getRecords())) {
            xhMedicationPlanResps = XhMedicationPlanConvertor.INSTANCE.entityListToRespList(page.getRecords());
            xhMedicationPlanResps.forEach(p -> {
                p.setProductDateList(Arrays.asList(p.getProductDate().split(",")));
            });
        }
        return PageResult.result(page.getTotal(), xhMedicationPlanResps);
    }

}



